package com.hmdp.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<ShopType> queryTypeList() {
        //查询Redis，有则返回。没有则查询数据库，存Redis返回，返回数据。
        String redisKey = RedisConstants.CACHE_SHOP_KEY + "type";
        String typeJson = (String)redisTemplate.opsForValue().get(redisKey);
        if(StrUtil.isNotBlank(typeJson)){
            List<ShopType> shopTypes = JSONUtil.toList(typeJson, ShopType.class);
            return shopTypes;
        }
        List<ShopType> shopTypes = baseMapper.selectList(null);
        if(!CollectionUtil.isEmpty(shopTypes)){
            typeJson = JSONUtil.toJsonStr(shopTypes);
            redisTemplate.opsForValue().set(redisKey,typeJson);
        }
        return shopTypes;
    }
}
